package com.cn.qtms.service.impl;//类注释设置模板

import com.cn.qtms.base.dao.BaseDao;
import com.cn.qtms.base.service.impl.BaseServiceImpl;
import com.cn.qtms.dao.TsAttachmentDao;
import com.cn.qtms.entity.TsAttachment;
import com.cn.qtms.exception.AppException;
import com.cn.qtms.service.TsAttachmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Name:TsAttachmentServiceImpl
 * @Description:
 * @Author: zhangyang
 * @Date: 2019-09-19 16:30
 **/
@Service
public class TsAttachmentServiceImpl extends BaseServiceImpl<TsAttachment, Long> implements TsAttachmentService {
    @Autowired
    private TsAttachmentDao dao;

    @Override
    public BaseDao<TsAttachment, Long> getDao() {
        return dao;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TsAttachment deleteAttachMent(String path) {
        TsAttachment attachment = dao.findAttachment(path);
        if(attachment!=null && attachment.getId()!=null){
            dao.delete(attachment);
            return attachment;
        }else{
            throw new AppException("路径不存在，删除失败");
        }

    }

    @Override
    public List<TsAttachment> findTsAttachments(String relatedTable, Long relatedId, String fileType) {
        return dao.findTsAttachments(relatedTable,relatedId,fileType);
    }

    @Override
    public List<String> findAttachments(String relatedTable, Long relatedId, String fileType) {
        List<TsAttachment> all = this.findTsAttachments(relatedTable, relatedId, fileType);
        List<String> attachments=new ArrayList<>();
        all.forEach(item->{
            if(item.getFileName()!=null){
                attachments.add(item.getFileName());
            }
        });
        return attachments;
    }

}
